// ==UserScript==
// @name           grill-spy
// @namespace      org.codegrill.ikariam
// @description    Displays spy defense information of a colony
//
//                 Author: brains
//                 Server: Theta
//                 Email: bwdenton@gmail.com
//                 Copyright 2009 Brian Denton
//
//                 This program is free software: you can redistribute it and/or modify
//                 it under the terms of the GNU General Public License as published by
//                 the Free Software Foundation, either version 3 of the License, or
//                 (at your option) any later version.
//
//                 This program is distributed in the hope that it will be useful,
//                 but WITHOUT ANY WARRANTY; without even the implied warranty of
//                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//                 GNU General Public License for more details <http://www.gnu.org/licenses/>.
//
// @include        http://*.ikariam.*
// @exclude        http://board.ikariam.*
// @require        http://ikariam-grill.googlecode.com/svn/tags/grill-debug/1.0/grill-debug.user.js
// @require        http://ikariam-grill.googlecode.com/svn/tags/grill-sdk/0.1/grill-sdk.user.js
// ==/UserScript==

(function () {
    try {
        grill_init();

        var colony = grill_getCurrentColony();
        var div = grill_spy_createSpyDiv(colony);

        if (div) {
            var inserted = grill_dom_insertElementAfterId(div, "information"); //In city view
            if (!inserted) {
                grill_dom_insertElementAfterId(div, "buildingUpgrade"); //In building view
            }
        }
    }
    catch (e) { GM_log(e); }
})();

/////////////////////////////////////////////////////////////////////
// Ikariam Spy UI Functions
/////////////////////////////////////////////////////////////////////
/**
 * Create the div to be inserted
 */
function grill_spy_createSpyDiv(colony) {
    if (colony != null && colony != undefined) {
        var building = colony.buildings[BUILDING_TYPE_HIDEOUT];
        var spies = grill_getSpies(grill_cache_getSpies(colony.server, colony.player, colony.id));
        var trained = grill_getTrainedSpies(grill_cache_getTrainedSpies(colony.server, colony.player, colony.id));
        var level = 0;

        if (building) {
            if (!spies) { spies = building.level; }
            if (!trained) { trained = building.level; }
            if (building) { level = building.level; }

            var defense = grill_spy_getSpyDefense(colony.level, level, spies, level);
            var cap = grill_spy_getEnemyCap(colony.level, level, spies);

            var xml = <>
            <h3 class="header">Ikariam Spy</h3>
            <div class="content">
                <ul style="text-align: left" class="cityinfo">
                    <li style="margin: 2px 10px;" id="ikariam-spy-defense" class="name">
                        <span style="float: left;text-align: left;width: 80px;" class="textLabel">Spy Defense: </span>
                        <span id="ikariam-spy-defense-value">{defense}%</span>
                    </li>
                    <li style="margin: 2px 10px;" id="ikariam-spies" class="name" alt="foo">
                        <span style="float: left;text-align:left;width:80px;" class="textLabel">Spies: </span>
                        <span id="ikariam-spy-cap-value">{spies}/{trained}</span>
                    </li>
                    <li style="margin: 2px 10px;" id="ikariam-spy-cap" class="name" alt="foo">
                        <span style="float: left;text-align:left;width:80px;" class="textLabel">Spy Cap: </span>
                        <span id="ikariam-spy-cap-value">Level {cap} enemy</span>
                    </li>
                </ul>
            </div>
            <div class="footer"/>
            </>; //An XMLList object

            var div = document.createElement("div");
            div.innerHTML = xml.toXMLString();

            return div;
        }
        else {
            GM_log("grill_spy_createSpyDiv(): Unable to get the hideout building");
        }
    }
    else {
        GM_log("grill_spy_createSpyDiv(): the colony is null");
    }

    return null;
}